package com.fitns.user.controller.baseConfig;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.fitns.user.conf.Resp;
import com.fitns.user.dto.baseConfig.TbGarbageBurnDateDTO;
import com.fitns.user.dto.baseConfig.TbGarbageBurnDateVO;
import com.fitns.user.model.baseConfig.TbGarbageBurnData;
import com.fitns.user.service.baseConfig.TbGarbageBurnDataService;
import com.fitns.user.utils.ExcelUtils;
import com.fitns.user.utils.UuidUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin //解决前后端对接跨域问题
@RequestMapping("backend")
@Api(description="垃圾焚烧场数据处理")
public class TbGarbageBurnDataController {

	@Autowired
	private TbGarbageBurnDataService tbGarbageBurnDateService;
	
	
	@PostMapping("insert/garbage")
	@ApiOperation("新增垃圾焚烧厂数据")
	public Object insertGarbageBurnDate(@Validated TbGarbageBurnDateDTO tbGarbageBurnDateDTO) {
		Integer insert = tbGarbageBurnDateService.insertGarbageBurnDate(tbGarbageBurnDateDTO);
		Resp resp =null;
		if(insert != 0) {
			resp = Resp.wrap("垃圾焚烧厂数据新增成功");
		}else {
			resp = Resp.errorWrap("垃圾焚烧厂数据添加失败");
		}
		return resp;
	}
	
	@PutMapping("update/garbage")
	@ApiOperation("修改垃圾焚烧厂数据")
	public Object updateGarbageBurnDate(@Validated TbGarbageBurnDateDTO tbGarbageBurnDateDTO) {
		Integer update  =  tbGarbageBurnDateService.updateGarbageBurnDate(tbGarbageBurnDateDTO);
		Resp resp = new Resp();
		if(update != 0) {
			resp.wrap("修改垃圾焚烧厂数据成功");
		}else {
			resp.errorWrap("修改垃圾焚烧厂数据失败");
		}
		return resp;
	}
	
	@DeleteMapping("delete/garbage")
	@ApiOperation("根据id删除垃圾焚烧厂数据")
	public Object deleteGarbageBurnDate(@RequestParam long id) {
		Integer delete = tbGarbageBurnDateService.deleteGarbageBurnDate(id);
		Resp resp = null;
		if(delete != 0) {
			resp = Resp.wrap("删除成功");
		}else {
			resp = Resp.errorWrap("删除失败");
		}
		return resp;
	}
	
	@GetMapping("select/garbageById")
	@ApiOperation("根据id查看垃圾焚烧厂数据")
	public Object selectGarbageBurnDate(long id) {
		
	TbGarbageBurnData garbageId =  tbGarbageBurnDateService.selectGarbageBurnDate(id);
		
	return garbageId;
	}
	
	@GetMapping("select/garbage")
	@ApiOperation("查询垃圾焚烧厂数据")
    public Object selectGarbageBurnDateList(@Validated TbGarbageBurnDateDTO tbGarbageBurnDateDTO) {
        PageInfo<TbGarbageBurnDateVO> bannerId = tbGarbageBurnDateService.selectGarbageBurnDateList(tbGarbageBurnDateDTO);
        bannerId.setPageSize(tbGarbageBurnDateDTO.getPageSize());
        return Resp.page(bannerId);
    }
	
	
	@GetMapping("export/garbage")
    @ApiOperation("exclue导出垃圾焚烧厂数据，8月8号原型取消垃圾焚烧厂数据导出功能")
	 public void exportCustomer(TbGarbageBurnDateDTO tbGarbageBurnDateDTO, HttpServletResponse response, HttpServletRequest request) throws Exception{
        List<List<String>> excelList =tbGarbageBurnDateService.export(tbGarbageBurnDateDTO);
        //String str="new sheet";
        ExcelUtils.export(response,request,"垃圾焚烧厂数据",excelList);
    }
	
	 /** 图片上传功能 */ 
		//postman测试以数组进行，用相同的key也就是传多个pic，不同的pic有不同的图片
	    @ApiOperation("上传图片")
	    @PostMapping("uplode/pic")
	    public String insertImage(TbGarbageBurnDateDTO tbGarbageBurnDateDTO, @RequestParam("pic") MultipartFile[] pic) throws Exception {
	        String images="";
	        // 把图片保存到图片目录下
	        for (int i=0;i<pic.length;i++) {
	            MultipartFile file=pic[i];
	            // 为每个文件生成一个新的文件名
	            String picName = UuidUtils.getUUID();
	            // 取文件扩展名
	            String oriName = file.getOriginalFilename();
	            String extName = oriName.substring(oriName.lastIndexOf("."));
	            // 保存文件
	            //file.transferTo(new File(Constants.IMAGE_PATH + picName + extName));
	            file.transferTo(new File("D:\\workspace\\woliu-zhhw\\zhhw\\src\\main\\resources\\image\\"+ picName + extName));
	           
	            images+=picName + extName+";";
	        }

	        if(images!=""){
	            images=images.substring(0,images.length()-1);
	        }
	        // 把文件名保存到数据库
	        tbGarbageBurnDateDTO.setImage(images);
	        tbGarbageBurnDateService.inserImage(tbGarbageBurnDateDTO);
	        return tbGarbageBurnDateDTO.getImage();
	    }
}
